﻿using BaseUI;
using BLL;
using Model;
using System;
using System.Data;

namespace Thksoft.Funds
{
    public partial class UrgentFApply : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //txtDate.Value = DateTime.Now.AddMonths(-1).ToString("yyyy-MM-dd");

                // txtEnd.Value = DateTime.Now.ToString("yyyy-MM-dd");

                //BindData(true);
                // Bind();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (FORM_COMPANY_ID.Value == -1)
            {
                MessageBox.Show(Page, "请选择公司");
                return;
            }

            if (txturgDate.Value.Trim() == "")
            {
                MessageBox.Show(Page, "请选择日期");
                return;
            }

            if (txtTitle.Value.Trim() == "")
            {
                MessageBox.Show(Page, "请填写业务名称");
                return;
            }

            if (txtDfzh.Value.Trim() == "")
            {
                MessageBox.Show(Page, "请输入对方账户");
                return;
            }

            if (txtDfzhName.Text.Trim() == "")
            {
                MessageBox.Show(Page, "请输入对方账户名称");
                return;
            }

            if (txtamont1.Value.Trim() == "")
            {
                MessageBox.Show(Page, "请输入打款金额");
                return;
            }

            QS_Business bModel = new QS_Business();

            decimal bid = new Qs_BusinessBLL().GetYearDid("seq_business_id");

            bModel.Business_Id = bid;
            bModel.Business_Name = txtTitle.Value.Trim();

            Lc_Process proModel = new Lc_ProcessBLL().GetProcessByCom("QS_URGENTFUNDSAPPLY", lognUser.USER_NAME);

            if (proModel == null)
            {
                MessageBox.Show(Page, "未设置紧急资金流程！");
                return;
            }
            bModel.Process_id = proModel.N_PROCESS_ID;// 121;
            bModel.Business_State = 1;
            bModel.Business_operator_Id = int.Parse(lognUser.USER_ID.ToString());
            bModel.Business_remark = txtRemark.Text.Trim();
            bModel.N_COMPANY_ID = FORM_COMPANY_ID.Value;

            QS_URGENTFUNDSAPPLY model = new QS_URGENTFUNDSAPPLY();

            model.N_AMOUNT = decimal.Parse(txtamont1.Value.Trim());
            model.C_ACCOUNT = txtDfzh.Value.Trim();
            model.C_ACCOUNT_NAME = txtDfzhName.Text.Trim();
            model.C_TITLE = txtTitle.Value.Trim();

            model.C_EVENT = Text1.Value.Trim();
            model.D_DATA = DateTime.Parse(txturgDate.Value);

            model.N_BUSINESS_ID = bid;
            model.N_BUSINESS_STATE = 1;

            model.N_USER_ID = lognUser.USER_ID;

            model.N_COMPANY_ID = FORM_COMPANY_ID.Value;

            model.C_REMARK = txtRemark.Text.Trim();

            Lc_Transaction tModel = new Lc_Transaction();
            tModel.N_PROCESS_ID = bModel.Process_id;

            //获取第一步队列id
            tModel.N_QUEUE_ID = new Lc_QueueBLL().QuByNumCom(bModel.Process_id, bModel.N_COMPANY_ID);
            tModel.N_business_id = bid;
            tModel.N_TRANSACTION_STATE = 1;
            tModel.C_TRANSACTION_COMMENT = "紧急资金申请";
            tModel.C_TRANSACTOR_ID = lognUser.USER_ID.ToString();
            tModel.D_TRANSACTION_TIME_BEGIN = DateTime.Now;
            tModel.D_TRANSACTION_TIME_END = DateTime.Now;
            tModel.D_TRANSACTION_TIME_DUE = DateTime.Parse("1900-1-1");
            tModel.N_TRANSACTION_STATE = 1;

            tModel.C_TRANSACTOR_SIGNATURE = "";
            tModel.N_LAST_BACE_TRANSACTION_ID = 0;

            Lc_Transaction NexModel = new Lc_Transaction();
            NexModel.N_PROCESS_ID = bModel.Process_id;

            DataTable dtNex = new Lc_ConditionBLL().GetNextNum(bModel.Process_id, tModel.N_QUEUE_ID);
            if (dtNex == null || dtNex.Rows.Count == 0)
            {
                MessageBox.Show(Page, "未设置办理流水！");
                return;
            }
            NexModel.N_QUEUE_ID = decimal.Parse(dtNex.Rows[0][0].ToString());
            NexModel.N_business_id = bid;
            NexModel.N_TRANSACTION_STATE = 0;
            NexModel.C_TRANSACTION_COMMENT = "";
            NexModel.C_TRANSACTOR_ID = "";
            NexModel.D_TRANSACTION_TIME_BEGIN = DateTime.Now;
            NexModel.D_TRANSACTION_TIME_END = DateTime.Parse("1900-1-1");
            Lc_Queue qModel = new Lc_QueueBLL().GetInfoById(NexModel.N_QUEUE_ID.ToString());
            Lc_Node nModel = new Lc_NodeBLL().GetInfoById(qModel.N_NODE_ID.ToString());
            NexModel.D_TRANSACTION_TIME_DUE = DateTime.Now.AddDays(double.Parse(nModel.N_DOIING_TIME.ToString()));
            NexModel.N_TRANSACTION_STATE = 0;
            NexModel.C_TRANSACTOR_SIGNATURE = "";
            NexModel.N_LAST_BACE_TRANSACTION_ID = 0;

            int count = new QS_URGENTFUNDSAPPLYBLL().AppUrgent(bModel, model, tModel, NexModel, lognUser.USER_NAME);

            if (count > 0)
            {
                MessageBox.Show(Page, "申请成功！");
            }
            else
            {
                MessageBox.Show(Page, "申请失败！");
            }
        }
    }
}